﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace HyperionS2P
{
    public class DocumentPDF
    {
        Document pdf = null;
        MemoryStream stream = new MemoryStream();
        PdfWriter writer = null;
        protected float margines = 36;

        public DocumentPDF()
        {
            pdf = new Document(PageSize.A4, margines, margines, margines, margines);
            writer = PdfWriter.GetInstance(pdf, stream);
            writer.CloseStream = false;
            pdf.Open();
            
        }

        public void AddImage(System.Drawing.Image image)
        {
            pdf.NewPage();
            Image imageToPdf = Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Bmp);
            imageToPdf.ScalePercent(72f / imageToPdf.DpiX * 100);
            imageToPdf.SetAbsolutePosition(0, 0); 
            pdf.Add(imageToPdf);
            
        }

        public void Save(string filename)
        {
            pdf.Close();
            writer.Close();

            using(FileStream fileStream = new FileStream(filename, FileMode.Create))
            {
                stream.Position = 0;
                stream.CopyTo(fileStream);
            }

            stream.Close();
        }
    }
}
